package com.youlu.campus.service.vote.convert;

import com.youlu.campus.entity.UserPO;
import com.youlu.campus.entity.WechatH5UserPO;
import com.youlu.campus.entity.vote.VoteOrganizationInfo;
import com.youlu.campus.entity.vote.VoteSimpleActivityEntity;
import com.youlu.campus.entity.vote.vo.OrgVoteInfoVo;
import com.youlu.campus.entity.vote.vo.UserVoteInfoVo;
import com.youlu.campus.entity.vote.vo.VoteActivityInfoVo;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Tuple;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * @author : [dengbo]
 * @className : ActivetyCourse
 * @description : [投票转化类]
 * @createTime : [2022/5/16 18:37]
 */
@Service
public class VoteOrgConvert {

    @Autowired
    private MapperFacade mapperFacade;

    public List<OrgVoteInfoVo> voteOrgToVo(Set<Tuple> rankList, List<VoteOrganizationInfo> list, long start) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        Map<String, VoteOrganizationInfo> map = list.stream().collect(Collectors.toMap(VoteOrganizationInfo::getOrgCode, Function.identity(), (o1, o2) -> o2));
        AtomicLong rankLong = new AtomicLong(start);
        return rankList.stream().map(item -> {
            VoteOrganizationInfo info = map.get(item.getElement());
            OrgVoteInfoVo vo;
            if (info == null) {
                vo = new OrgVoteInfoVo();
                // 设置排行
                setOrgVoteRank(vo, rankLong.longValue(), item.getScore());
            } else {
                vo = orgToUserOrgVo(info, rankLong.longValue(), item.getScore());
            }
            rankLong.incrementAndGet();
            return vo;
        }).collect(Collectors.toList());
    }


    public List<UserVoteInfoVo> voteUserToVo(Set<Tuple> rankList, List<WechatH5UserPO> list, long start) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        Map<String, WechatH5UserPO> map = list.stream().collect(Collectors.toMap(WechatH5UserPO::getAccountInfoId, Function.identity(), (o1, o2) -> o2));
        AtomicLong rankLong = new AtomicLong(start);
        return rankList.stream().map(item -> {
            WechatH5UserPO info = map.get(item.getElement());
            UserVoteInfoVo vo;
            if (info == null) {
                vo = new UserVoteInfoVo();
                // 设置排行
                setUserVoteRank(vo, rankLong.get(), item.getScore());
            } else {
                vo = h5UserToOrgVo(info, rankLong.get(), item.getScore());
            }
            rankLong.incrementAndGet();
            return vo;
        }).collect(Collectors.toList());
    }

    public UserVoteInfoVo h5UserToOrgVo(WechatH5UserPO userPO, Long rank, Double score) {
        UserVoteInfoVo userVoteInfo = new UserVoteInfoVo();
        userVoteInfo.setUserId(userPO.getAccountInfoId());
        userVoteInfo.setUniversityId(userPO.getUniversityId());
        userVoteInfo.setOpenId(userPO.getOpenId());
        userVoteInfo.setAvatarUrl(userPO.getAvatarUrl());
        userVoteInfo.setNickName(userPO.getNickName());
        userVoteInfo.setProvince(userPO.getProvince());
        // 设置排行
        setUserVoteRank(userVoteInfo, rank, score);
        return userVoteInfo;
    }

    public UserVoteInfoVo acUserToOrgVo(UserPO userPO, Long rank, Double score) {
        UserVoteInfoVo userVoteInfo = new UserVoteInfoVo();
        userVoteInfo.setUserId(userPO.getId());
        userVoteInfo.setOpenId(userPO.getOpenId());
        userVoteInfo.setAvatarUrl(userPO.getAvatarUrl());
        userVoteInfo.setNickName(userPO.getNickName());
        userVoteInfo.setProvince(userPO.getProvince());
        userVoteInfo.setUniversityId(userPO.getUniversityId());
        // 设置排行
        setUserVoteRank(userVoteInfo, rank, score);
        return userVoteInfo;
    }

    public OrgVoteInfoVo orgToUserOrgVo(VoteOrganizationInfo organizationInfo, Long rank, Double score) {
        OrgVoteInfoVo infoVo = orgToVoteInfoVo(organizationInfo);
        // 设置排行
        setOrgVoteRank(infoVo, rank, score);
        return infoVo;
    }

    public void setOrgVoteRank(OrgVoteInfoVo vo, Long rank, Double score) {
        Long scoreRank = (score == null || score.longValue() <= 0) ? null : rank + 1;
        vo.setRank(scoreRank);
        vo.setVoteNum((score == null ? 0 : score.longValue()));
    }

    public void setUserVoteRank(UserVoteInfoVo vo, Long rank, Double score) {
        Long scoreRank = (score == null || score.longValue() <= 0) ? null : rank + 1;
        vo.setRank(scoreRank);
        vo.setVoteNum((score == null ? 0 : score.longValue()));
    }

    public VoteActivityInfoVo voteActivityToVo(VoteSimpleActivityEntity activityInfo) {
        return mapperFacade.map(activityInfo, VoteActivityInfoVo.class);
    }

    public OrgVoteInfoVo orgToVoteInfoVo(VoteOrganizationInfo organizationInfo) {
        OrgVoteInfoVo infoVo = new OrgVoteInfoVo();
        infoVo.setOrgId(organizationInfo.getOrgId());
        infoVo.setOrgCode(organizationInfo.getOrgCode());
        infoVo.setOrgLogo(organizationInfo.getOrgLogo());
        infoVo.setOrgName(organizationInfo.getOrgName());
        infoVo.setProvince(organizationInfo.getProvince());
        infoVo.setProvinceCode(organizationInfo.getProvinceCode());
        return infoVo;
    }

}
